import constants as con
import pygame
import pymunk 
import math, sys, random

class Coin(pygame.sprite.Sprite):
    image = None

    def __init__(self,parent):
        pygame.sprite.Sprite.__init__(self)

        self.parent = parent

        if Coin.image is None:
            Coin.image = con.load_image('dat_coin.png')

        self.plink = con.load_sound('Coin.wav')

        #self.width = int(con.window_size[0] * 0.024375)
        #self.height = int(con.window_size[1] * 0.03125)
        self.width = int(con.window_size[0] * 0.015625)
        self.height = int(con.window_size[1] * 0.0209)
        self.radius = int(self.width / 2)

        self.image = self.master = pygame.transform.scale(Coin.image, (self.width, self.height))

        self.friction = 0.5
        self.inertia = 10
        self.mass = 0.08
        self.elasticity = 0.5

        self.centerX = int(con.window_size[0] / 2)
        self.centerY = int((con.window_size[1] / 2) * 0.7)

        self.body = pymunk.Body(self.mass, self.inertia)

        modX = random.randint( int(con.window_size[0] * 0.1171875),int(con.window_size[0] * 0.8828125) )
        modY = random.randint(1,int(con.window_size[1] * 0.1)) - int(con.window_size[1] * 0.05)

        self.body.position = modX, self.centerY + (con.window_size[1] * 0.5) + modY

        self.shape = pymunk.Circle(self.body, self.radius, (0,0))
        self.shape.friction = self.friction
        self.shape.layers = 2 | 4
        self.shape.type = 'coin'
        self.shape.elasticity = self.elasticity
        self.shape.parent = self

        self.rect = pygame.Rect(0,0,self.width,self.height)
        self.rect.center = con.to_pygame(self.body.position)

        self.parent.space.add(self.body, self.shape)
        self.parent.sprites.add(self)

    def update(self):
        center = self.rect.center
        self.image = pygame.transform.rotate(self.master, math.degrees(self.body.angle))
        self.rect = self.image.get_rect()
        self.rect.center = con.to_pygame(self.body.position)

    def purge(self):
        self.parent.space.remove(self.shape,self.body)
        self.parent.sprites.remove(self)
        self.parent.coinlist.remove(self)

    def pymunkDraw(self,screen):
        p = con.to_pygame(self.body.position)
        pygame.draw.circle(screen, (255,255,255), p, self.radius, 2)

    def play(self):
        self.plink.play()
